package ch.wanandroid;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.wanandroid.api.HomePageApi;
import ch.wanandroid.comr.wanandroid.R;
import ch.wanandroid.lib.database.BannerResponseDatabase;
import ch.wanandroid.lib.net.beans.BannerResponse;
import ch.wanandroid.view.HomeFragment;
import ch.wanandroid.view.NavigationFragment;
import ch.wanandroid.view.ProjectFragment;
import ch.wanandroid.view.SystemFragment;
import ch.wanandroid.lib.net.RetrofitClient;
import ch.wanandroid.lib.net.beans.ArticleResponse;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {

    Retrofit mRetrofit;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationView mBottomNavigationView;
    ViewPager mViewPager;
    private List<Fragment> mFragmentList=new ArrayList<>();
    boolean isBottomShow = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mViewPager=findViewById(R.id.view_pager);
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new SystemFragment());
        mFragmentList.add(new NavigationFragment());
        mFragmentList.add(new ProjectFragment());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mFragmentList.get(i);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                mBottomNavigationView.getMenu().getItem(i).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.navigation_home:{
                        mViewPager.setCurrentItem(0);
                        return true;
                    }
                    case R.id.navigation_system:{
                        mViewPager.setCurrentItem(1);
                        return true;
                    }
                    case R.id.navigation_navigation:{
                        mViewPager.setCurrentItem(2);
                        return true;
                    }
                    case R.id.navigation_project:{
                        mViewPager.setCurrentItem(3);
                        return true;
                    }
                }
                return false;
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("sdfsdff","start");
                BannerResponseDatabase.getInstance(MainActivity.this).getBannerResponseDao().deleteBanner();
                BannerResponse.Banner bannerResponse=new  BannerResponse.Banner();
                bannerResponse.setTitle("abc");
                bannerResponse.setId(1);
                BannerResponse.Banner bannerResponse1=new  BannerResponse.Banner();
                bannerResponse1.setTitle("cdg");
                bannerResponse1.setId(4);
                BannerResponseDatabase.getInstance(MainActivity.this).getBannerResponseDao().insertBanner(bannerResponse,bannerResponse1);
                List<BannerResponse.Banner> bannerList=BannerResponseDatabase.getInstance(MainActivity.this).getBannerResponseDao().getAllBanners();
                for (int i=0;i<bannerList.size();i++){
                    Log.d("sdfsdff","titile="+bannerList.get(i).getTitle());
                }
            }
        }).start();
        /*mRetrofit = RetrofitClient.getInstance();
        HomePageApi api = mRetrofit.create(HomePageApi.class);
        api.getFriendWebsite()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FriendWebsiteResponse>() {
                    @Override
                    public void accept(FriendWebsiteResponse articleResponse) throws Exception {
                        int  size = articleResponse.getData().size();
                        Log.d("sdfsfsaf","size"+size);
                    }
                });*/
        //testApi();
    }


    public void testApi() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mRetrofit = RetrofitClient.getInstance();
                HomePageApi api = mRetrofit.create(HomePageApi.class);
                api.getHomeArticle(0)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<ArticleResponse>() {
                            @Override
                            public void accept(ArticleResponse articleResponse) throws Exception {
                                int size = articleResponse.getData().getSize();
                            }
                        });
            }
        }).start();
    }

}
